package com.pww.movie.controller;



import com.pww.movie.domain.PO.User;
import com.pww.movie.domain.VO.UserVO;
import com.pww.movie.service.IUserService;
import com.pww.movie.utils.Result;
import com.pww.movie.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author pww
 * @since 2025-05-22
 */
@AllArgsConstructor
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;


    /**
     * 用户注册
     *
     * @param
     * @return
     */
    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^.{2,16}$") String username, @Pattern(regexp = "^[a-zA-Z0-9]{2,16}$") String password) {
        log.info("用户名:{},密码:{}", username, password);
        //判断用户名是否在数据库中存在
        User user = userService.lambdaQuery().eq(User::getUsername, username).one();
        if (user != null) {
            return Result.error("用户名已存在");
        } else {
            //说明用户名不存在 开始注册
            return userService.register(username, password);
        }
    }

    /**
     * 根据用户id获取用户信息
     *
     * @return
     */
    @GetMapping("/getUserInfo")
    public Result<UserVO> getUserInFo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        System.out.println("username = " + username);
        return userService.getByUserName(username);
    }
}
